#include <bits/stdc++.h>
// 2025/04/04
// tag: 
// Author: Forever
using namespace std;
using int64 = int64_t;

int dp[105][105];
string a, b;

int Dist(char a, char b)
{
    int dist[5][5] = {
        {5, -1, -2, -1, -3},
        {-1, 5, -3, -2, -4},
        {-2, -3, 5, -2, -2},
        {-1, -2, -2, 5, -1},
        {-3, -4, -2, -1, 0}
    };
    int x = a == 'A' ? 0 : a == 'C' ? 1 : a == 'G' ? 2 : a == 'T' ? 3 : 4;
    int y = b == 'A' ? 0 : b == 'C' ? 1 : b == 'G' ? 2 : b == 'T' ? 3 : 4;
    return dist[x][y];
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n1, n2;
    cin >> n1 >> a >> n2 >> b;
    a = '-' + a;
    b = '-' + b;
    for (int i = 1; i <= n1; i++)
        dp[i][0] = dp[i - 1][0] + Dist(a[i], '-');
    for (int j = 1; j <= n2; j++)
        dp[0][j] = dp[0][j - 1] + Dist(b[j], '-');
    dp[0][0] = 0;
    for (int i = 1; i <= n1; i++)
        for (int j = 1; j <= n2; j++)
            dp[i][j] = max({dp[i - 1][j] + Dist(a[i], '-'), dp[i][j - 1] + Dist(b[j], '-'), dp[i - 1][j - 1] + Dist(a[i], b[j])});
    cout << dp[n1][n2] << endl;
    return 0;
}